<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png"
             alt=""
             height="50px">
        <span>电商管理信息系统</span>
      </div>
      <el-button type="info"
                 @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏菜单区域 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="toggle-button"
             @click="toggleCollapse">|||</div>
        <!-- index属性只支持传入字符串 -->
        <el-menu background-color="#333744"
                 text-color="#fff"
                 active-text-color="#409bff"
                 :unique-opened="true"
                 :collapse="iscollapse"
                 :collapse-transition="false"
                 :router="true"
                 :default-active="$route.path">
          <!-- 一级菜单 -->
          <el-submenu :index="index+''"
                      v-for="(item, index) in menuList"
                      :key="index">
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单-->
            <el-menu-item :index="'/'+subItem.path"
                          v-for="(subItem, subIndex) in item.children"
                          :key="subIndex">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体区域 -->
      <el-main>
        <!-- 子路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      iscollapse: false, // 是否折叠
      menuList: [] // 左侧菜单列表
    }
  },
  created () {
    this.getMenuList()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/')
    },
    async getMenuList () {
      // getMenuList: async () => {}  如此无法获取到this
      const { data: res } = await this.$http.get('menus')
      console.log(res)
      if (res.status !== 200) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.menuList = res.data
    },
    toggleCollapse () {
      // 侧边栏菜单折叠/展开
      this.iscollapse = !this.iscollapse
    }
  }
}
</script>

<style lang="less" scoped>
// 这里el-container el-header el-aside等element组件渲染后的类目和标签名称一致
.el-container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .toggle-button {
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-menu {
    border: none;
  }
}

.el-main {
  background-color: #eaedf1;
}
</style>
